<template>
  <div class="employees-container">
    <mytools class="mytools" :showleft="true">
      <template #left>
        员工信息
      </template>
      <template #right>
        <el-button
          type="primary"
          size="mini"
          @click="$router.push('/employees/myExcel')"
          >导入</el-button
        >
        <el-button type="primary" size="mini" @click="showDialog"
          >+ 新增员工</el-button
        >
      </template>
    </mytools>
    <el-card class="mycard">
      <el-table :data="tableData" style="width: 100%" border>
        <el-table-column prop="" label="序号" type="index" width="80">
        </el-table-column>
        <el-table-column prop="username" label="姓名" width="160">
        </el-table-column>
        <el-table-column label="图片" width="160">
          <template slot-scope="{ row }">
            <img
              :src="row.staffPhoto"
              alt=""
              @click="previewImg(row.staffPhoto)"
              v-imgerror="defaulting"
              class="myimg"
            />
          </template>
        </el-table-column>
        <el-table-column prop="mobile" label="手机号" width="160">
        </el-table-column>
        <el-table-column prop="workNumber" label="工号" width="160">
        </el-table-column>
        <el-table-column label="聘用形式" width="160">
          <template slot-scope="{ row }">
            {{ row.formOfEmployment | myEmployees }}
          </template>
        </el-table-column>
        <el-table-column prop="departmentName" label="部门" width="160">
        </el-table-column>
        <el-table-column prop="timeOfEntry" label="入职时间" width="160">
        </el-table-column>
        <el-table-column label="是否在职" width="160">
          <template>在职</template>
        </el-table-column>
        <el-table-column label="状态" width="160">
          <template>可用</template>
        </el-table-column>
        <el-table-column prop="" label="操作" width="280" fixed="right">
          <template slot-scope="{ row }">
            <el-button type="text" @click="toDetail(row.id)">查看</el-button>
            <el-button type="text">转正</el-button>
            <el-button type="text">调岗</el-button>
            <el-button type="text">离职</el-button>
            <el-button type="text" @click="toRole(row.id)">角色</el-button>
            <el-button type="text">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        style="text-align:right"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="pagesizes"
        :page-size="pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
    <penel
      :dialogShow.sync="dialogShow"
      @getEmployeeInfo="getEmployeeInfo"
    ></penel>
    <el-dialog
      title="分配角色"
      :visible.sync="dialogVisible"
    >
     <!-- <div>{{checkList.id}}</div> -->
     <el-form>
       <el-form-item>
         <el-checkbox-group v-model="checkList" class="mycheckbox">
        <el-checkbox v-for="(item,index) in roleList" :key="index" :label="item.id">{{item.name}}</el-checkbox>
      </el-checkbox-group>
       </el-form-item>
       <el-form-item class="rolebtn"> 
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </el-form-item>
     </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { getEmployeesList, getEmployees, assignRolesApi} from '@/api/employees'
import { getRolesApi } from '@/api/roles'
export default {
  name: 'employees',
  data () {
    return {
      tableData: [],
      currentPage: 1,
      pagesizes: [5, 10, 15, 20],
      pagesize: 5,
      total: 0,
      dialogShow: false,
      defaulting: require('@/assets/common/head.jpg'),
      show: false,
      mysrc: '',
      dialogVisible: false,
      checkList: [],
      roleList:[],
      currentId:''
    }
  },
  components: {
    penel: () => import('./components/penel.vue')
  },
  created () {
    this.getEmployeeInfo()
    this.getRoles()
  },
  methods: {
    async submit(){
      await assignRolesApi({
        id:this.currentId,
        roleIds:this.checkList
      })
      this.$message.success('修改成功')
      this.dialogVisible = false
      this.getEmployeeInfo()

    },
    //跳转查看页面
    toDetail (id) {
      this.$router.push({ path: '/employees/detail', query: { id } })
      // this.$router.push({name:'detail',query:{id}})
    },
    //显示弹窗
    showDialog () {
      this.dialogShow = true
    },
    //页容量变化
    handleSizeChange (val) {
      // console.log(val)
      this.pagesize = val
      this.getEmployeeInfo()
    },
    handleCurrentChange (val) {
      // console.log(val)
      this.currentPage = val
      this.getEmployeeInfo()
    },
    //获取员工列表
    async getEmployeeInfo () {
      let res = await getEmployeesList({
        page: this.currentPage,
        size: this.pagesize
      })
      // console.log(res)
      this.tableData = res.data.rows
      this.total = res.data.total
    },
    //预览图片
    previewImg (src) {
      this.show = true
    },
    //角色分配
     async toRole (id) {
       this.currentId = id
      this.dialogVisible = true
      let res = await getEmployees(id)
      console.log(res);
      //后台返回的数据有bug，返回为null时需判断
      if(res.data.roleIds != null){
        this.checkList = res.data.roleIds
      }else{
        this.checkList = []
      }
      
    },
    //获取角色信息
    async getRoles(){
      let res = await getRolesApi()
      // console.log(res);
      this.roleList = res.data.rows
    }
  }
}
</script>

<style lang="scss">
.employees-container {
  margin: 20px;
  .el-card__body {
    padding: 15px;
  }
  .mycard {
    margin-top: 20px;
  }
  .myimg {
    width: 50px;
  }
  .mycheckbox{
    margin-bottom: 25px;
  }
  .rolebtn{
    text-align: center;
  }
}
</style>
